using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Text.Json;
using Catastrophe.Common.AttributeEx;
using Catastrophe.Common.Extensions;

namespace Catastrophe.Common
{
	public static class DataConverter
	{
		public static List<T> ConvertToEntity<T>(DataTable dt, int dstCount = -1, Func<PropertyInfo, string> func = null) where T : class, new()
		{
			if (dt == null || dt.Rows.Count == 0)
			{
				return new List<T>();
			}
			List<T> list = new List<T>();
			Type typeFromHandle = typeof(T);
			PropertyInfo[] properties = typeFromHandle.GetProperties();
			int num = dstCount;
			if (dstCount <= 0 || dstCount > dt.Rows.Count)
			{
				num = dt.Rows.Count;
			}
			for (int i = 0; i < num; i++)
			{
				T val = new T();
				DataRow dataRow = dt.Rows[i];
				PropertyInfo[] array = properties;
				foreach (PropertyInfo propertyInfo in array)
				{
					RowIndexAttribute rowIndexAttribute = Attribute.GetCustomAttribute(propertyInfo, typeof(RowIndexAttribute)) as RowIndexAttribute;
					if (rowIndexAttribute != null && propertyInfo.PropertyType == typeof(int))
					{
						propertyInfo.SetValue(val, i + rowIndexAttribute.StartIndex);
						continue;
					}
					string text = propertyInfo.Name;
					if (func != null)
					{
						string text2 = func(propertyInfo);
						if (!string.IsNullOrWhiteSpace(text2))
						{
							text = text2;
						}
					}
					if (dt.Columns.Contains(text) && dataRow[text] != DBNull.Value)
					{
						DataConverter.PropertySetValue(val, propertyInfo, dataRow[text]);
					}
				}
				list.Add(val);
			}
			return list;
		}

		public static List<T> ConvertToEntity<T>(DataTable dt, bool checkAdded, bool checkDeleted) where T : class, new()
		{
			List<T> list = new List<T>();
			foreach (DataRow row in dt.Rows)
			{
				DataRowState rowState = row.RowState;
				if (checkAdded)
				{
					if (checkDeleted)
					{
						if (!rowState.HasFlag(DataRowState.Added) || rowState.HasFlag(DataRowState.Deleted))
						{
							continue;
						}
					}
					else if (!rowState.HasFlag(DataRowState.Added))
					{
						continue;
					}
				}
				list.Add(DataConverter.ConvertToEntity<T>(row));
			}
			return list;
		}

		public static T ConvertToEntity<T>(DataRow row, Func<MemberInfo, string> func = null) where T : class, new()
		{
			if (row == null)
			{
				return null;
			}
			Type typeFromHandle = typeof(T);
			T val = new T();
			MemberInfo[] members = typeFromHandle.GetMembers();
			foreach (MemberInfo memberInfo in members)
			{
				if (memberInfo.MemberType != MemberTypes.Property)
				{
					continue;
				}
				PropertyInfo property = typeFromHandle.GetProperty(memberInfo.Name);
				string columnName = memberInfo.Name;
				if (func != null)
				{
					string text = func(memberInfo);
					if (!string.IsNullOrWhiteSpace(text))
					{
						columnName = text;
					}
				}
				if (row[columnName] != null && row[columnName] != DBNull.Value && !(property == null))
				{
					DataConverter.PropertySetValue(val, property, row[columnName]);
				}
			}
			return val;
		}

		public static T ConvertToEntity<T>(Dictionary<string, object> dic) where T : class, new()
		{
			PropertyInfo[] properties = typeof(T).GetProperties();
			T val = new T();
			foreach (string each in dic.Keys)
			{
				List<PropertyInfo> list = properties.Where((PropertyInfo x) => x.Name == each).ToList();
				if (list.Count > 0)
				{
					DataConverter.PropertySetValue(val, list.First(), dic[each]);
				}
			}
			return val;
		}

		public static DataTable ConvertToDataTable(Dictionary<string, object> dic)
		{
			if (dic == null || dic.Count == 0)
			{
				return new DataTable();
			}
			DataTable dataTable = new DataTable();
			dataTable.Columns.Add(new DataColumn("Name", typeof(string)));
			dataTable.Columns.Add(new DataColumn("Value"));
			foreach (string key in dic.Keys)
			{
				DataRow dataRow = dataTable.NewRow();
				dataRow["Name"] = key;
				dataRow["Value"] = dic[key];
				dataTable.Rows.Add(dataRow);
			}
			return dataTable;
		}

		public static DataTable ConvertToDataFrame<T>(bool checkExcluded = false)
		{
			PropertyInfo[] properties = typeof(T).GetProperties();
			DataTable dataTable = new DataTable();
			PropertyInfo[] array = properties;
			foreach (PropertyInfo propertyInfo in array)
			{
				if (!checkExcluded || !DataConverter.IsExcluded(propertyInfo))
				{
					Type underlyingType = Nullable.GetUnderlyingType(propertyInfo.PropertyType);
					Type dataType = underlyingType ?? propertyInfo.PropertyType;
					DataColumn column = new DataColumn(propertyInfo.Name, dataType);
					dataTable.Columns.Add(column);
				}
			}
			return dataTable;
		}

		public static void DtRowAdd(ref DataTable dt, DataRow row)
		{
			for (int i = 0; i < dt.Columns.Count; i++)
			{
				DataRow dataRow = dt.NewRow();
				string columnName = dt.Columns[i].ColumnName;
				if (row[columnName] != null)
				{
					dataRow[columnName] = row[columnName];
				}
				dt.Rows.Add(dataRow);
			}
		}

		public static void DtRowAdd(ref DataTable dt, DataRow[] rowBuf)
		{
			foreach (DataRow dataRow in rowBuf)
			{
				DataRow dataRow2 = dt.NewRow();
				for (int j = 0; j < dt.Columns.Count; j++)
				{
					string columnName = dt.Columns[j].ColumnName;
					if (dataRow[columnName] != null)
					{
						dataRow2[columnName] = dataRow[columnName];
					}
				}
				dt.Rows.Add(dataRow2);
			}
		}

		public static void DtRowAdd<T>(ref DataRow row, T value, bool checkExcluded = false)
		{
			if (value == null)
			{
				throw new ArgumentNullException("value");
			}
			Type typeFromHandle = typeof(T);
			PropertyInfo[] properties = typeFromHandle.GetProperties();
			foreach (PropertyInfo propertyInfo in properties)
			{
				if ((!checkExcluded || !DataConverter.IsExcluded(propertyInfo)) && row.Table.Columns.Contains(propertyInfo.Name) && row[propertyInfo.Name] != null)
				{
					row[propertyInfo.Name] = propertyInfo.GetValue(value);
				}
			}
		}

		public static void DtRowAdd<T>(ref DataTable dt, T value, bool checkExcluded = false)
		{
			DataRow dataRow = dt.NewRow();
			Type typeFromHandle = typeof(T);
			PropertyInfo[] properties = typeFromHandle.GetProperties();
			foreach (PropertyInfo propertyInfo in properties)
			{
				if ((!checkExcluded || !DataConverter.IsExcluded(propertyInfo)) && dataRow[propertyInfo.Name] != null)
				{
					dataRow[propertyInfo.Name] = propertyInfo.GetValue(value);
				}
			}
			dt.Rows.Add(dataRow);
		}

		public static void DtRowAdd<T>(ref DataTable dt, T[] valueBuf, bool checkExcluded = false)
		{
			Type typeFromHandle = typeof(T);
			foreach (T val in valueBuf)
			{
				DataRow dataRow = dt.NewRow();
				PropertyInfo[] properties = typeFromHandle.GetProperties();
				foreach (PropertyInfo propertyInfo in properties)
				{
					if ((!checkExcluded || !DataConverter.IsExcluded(propertyInfo)) && dataRow[propertyInfo.Name] != null)
					{
						dataRow[propertyInfo.Name] = propertyInfo.GetValue(val);
					}
				}
				dt.Rows.Add(dataRow);
			}
		}

		public static DataTable ConvertToDataT<T>(IEnumerable<T> collection, bool checkExcluded = false)
		{
			PropertyInfo[] properties = typeof(T).GetProperties();
			DataTable dataTable = new DataTable();
			dataTable.Columns.AddRange(properties.Select((PropertyInfo p) => new DataColumn(p.Name)).ToArray());
			if (collection.Any())
			{
				for (int i = 0; i < collection.Count(); i++)
				{
					ArrayList arrayList = new ArrayList();
					PropertyInfo[] array = properties;
					foreach (PropertyInfo propertyInfo in array)
					{
						if (!checkExcluded || !DataConverter.IsExcluded(propertyInfo))
						{
							string text = propertyInfo.GetValue(collection.ElementAt(i), null)?.ToString();
							text = text ?? "";
							arrayList.Add(text);
						}
					}
					object[] values = arrayList.ToArray();
					dataTable.LoadDataRow(values, fAcceptChanges: true);
				}
			}
			return dataTable;
		}

		public static List<T> ConvertToAry<T>(DataTable dt, string colName)
		{
			List<T> list = new List<T>();
			if (dt == null || !dt.Columns.Contains(colName))
			{
				return list;
			}
			for (int i = 0; i < dt.Rows.Count; i++)
			{
				T val = default(T);
				try
				{
					val = (T)dt.Rows[i][colName];
					list.Add(val);
				}
				catch
				{
				}
			}
			return list;
		}

		private static bool IsExcluded(PropertyInfo propertyInfo)
		{
			return propertyInfo.GetCustomAttribute(typeof(ExcludeAttribute), inherit: false) is ExcludeAttribute;
		}

		public static Dictionary<string, object> ConvertToDic(DataTable dt)
		{
			if (dt == null)
			{
				throw new ArgumentNullException("dt");
			}
			if (!dt.Columns.Contains("Name") || !dt.Columns.Contains("Value"))
			{
				throw new Exception("目标列不存在Name或Value");
			}
			Dictionary<string, object> dictionary = new Dictionary<string, object>();
			foreach (DataRow row in dt.Rows)
			{
				string text = row["Name"].ToString()!.Trim();
				if (text != "")
				{
					dictionary.Add(text, row["Value"]);
				}
			}
			return dictionary;
		}

		public static Dictionary<string, object> ConvertToDic<T>(T t, bool checkExcluded = false) where T : class, new()
		{
			Dictionary<string, object> dictionary = new Dictionary<string, object>();
			Type type = t.GetType();
			PropertyInfo[] properties = type.GetProperties();
			PropertyInfo[] array = properties;
			foreach (PropertyInfo propertyInfo in array)
			{
				if (!checkExcluded || !DataConverter.IsExcluded(propertyInfo))
				{
					dictionary.Add(propertyInfo.Name, propertyInfo.GetValue(t));
				}
			}
			return dictionary;
		}

		public static Dictionary<string, object> ConvertToDic<T>(List<T> tBuf, bool checkExcluded = false)
		{
			Dictionary<string, object> dictionary = new Dictionary<string, object>();
			Type typeFromHandle = typeof(T);
			PropertyInfo[] properties = typeFromHandle.GetProperties();
			foreach (T item in tBuf)
			{
				PropertyInfo[] array = properties;
				foreach (PropertyInfo propertyInfo in array)
				{
					if (!checkExcluded || !DataConverter.IsExcluded(propertyInfo))
					{
						dictionary.Add(propertyInfo.Name, propertyInfo.GetValue(item));
					}
				}
			}
			return dictionary;
		}

		public static DataTable DtCombine(DataTable dt1, DataTable dt2, string dtNameSpaceColName = "Source")
		{
			DataTable dataTable = new DataTable();
			foreach (DataColumn column2 in dt1.Columns)
			{
				if (dt2.Columns.Contains(column2.ColumnName))
				{
					DataColumn column = new DataColumn(column2.ColumnName, typeof(string));
					dataTable.Columns.Add(column);
				}
			}
			dataTable.Columns.Add(dtNameSpaceColName, typeof(string));
			foreach (DataRow row in dt1.Rows)
			{
				DataRow dataRow2 = dataTable.NewRow();
				foreach (DataColumn column3 in dataTable.Columns)
				{
					if (!(column3.ColumnName == dtNameSpaceColName))
					{
						dataRow2[column3.ColumnName] = row[column3.ColumnName];
					}
				}
				dataRow2[dtNameSpaceColName] = dt1.Namespace;
				dataTable.Rows.Add(dataRow2);
			}
			foreach (DataRow row2 in dt2.Rows)
			{
				DataRow dataRow4 = dataTable.NewRow();
				foreach (DataColumn column4 in dataTable.Columns)
				{
					if (!(column4.ColumnName == dtNameSpaceColName))
					{
						dataRow4[column4.ColumnName] = row2[column4.ColumnName];
					}
				}
				dataRow4[dtNameSpaceColName] = dt2.Namespace;
				dataTable.Rows.Add(dataRow4);
			}
			return dataTable;
		}

		public static DataTable DtCombineMulti(params DataTable[] buff)
		{
			return DataConverter.DtCombineMulti("DtSource", buff);
		}

		public static DataTable DtCombineMulti(string dtNameSpaceColName = "DtSource", params DataTable[] dtBuf)
		{
			if (dtBuf == null)
			{
				throw new ArgumentNullException("dtBuf");
			}
			if (dtBuf.Count() == 0)
			{
				return new DataTable();
			}
			if (dtBuf.Count() == 1)
			{
				return dtBuf.First();
			}
			DataTable dataTable = new DataTable();
			foreach (DataColumn column2 in dtBuf.First().Columns)
			{
				DataColumn column = new DataColumn(column2.ColumnName, typeof(string));
				dataTable.Columns.Add(column);
			}
			dataTable.Columns.Add(dtNameSpaceColName, typeof(string));
			foreach (DataTable dataTable2 in dtBuf)
			{
				foreach (DataRow row in dataTable2.Rows)
				{
					DataRow dataRow2 = dataTable.NewRow();
					foreach (DataColumn column3 in dataTable.Columns)
					{
						if (!(column3.ColumnName == dtNameSpaceColName) && dataTable2.Columns.Contains(column3.ColumnName))
						{
							dataRow2[column3.ColumnName] = row[column3.ColumnName];
						}
					}
					dataRow2[dtNameSpaceColName] = dataTable2.Namespace;
					dataTable.Rows.Add(dataRow2);
				}
			}
			return dataTable;
		}

		public static TDst DeepCopy<TSrc, TDst>(TSrc source) where TSrc : class, new()where TDst : class, new()
		{
			TDst val = new TDst();
			DataConverter.DeepCopy(source, val);
			return val;
		}

		public static void DeepCopy<TSrc, TDst>(TSrc src, TDst dst) where TSrc : class, new()where TDst : class, new()
		{
			if (src == null)
			{
				throw new ArgumentNullException();
			}
			if (dst == null)
			{
				dst = new TDst();
			}
			PropertyInfo[] properties = typeof(TSrc).GetProperties(BindingFlags.Instance | BindingFlags.Public);
			PropertyInfo[] properties2 = typeof(TDst).GetProperties(BindingFlags.Instance | BindingFlags.Public);
			PropertyInfo[] array = properties;
			foreach (PropertyInfo each in array)
			{
				PropertyInfo propertyInfo = properties2.FirstOrDefault((PropertyInfo x) => x.Name == each.Name);
				if (propertyInfo != null && !(Attribute.GetCustomAttribute(each, typeof(ExcludeAttribute)) is ExcludeAttribute))
				{
					object value = each.GetValue(src);
					DataConverter.PropertySetValue(dst, propertyInfo, value);
				}
			}
		}

		public static T DeepCopy<T>(T src) where T : class, new()
		{
			if (src == null)
			{
				throw new ArgumentNullException();
			}
			T val = new T();
			PropertyInfo[] properties = typeof(T).GetProperties(BindingFlags.Instance | BindingFlags.Public);
			PropertyInfo[] array = properties;
			foreach (PropertyInfo propertyInfo in array)
			{
				if (!(Attribute.GetCustomAttribute(propertyInfo, typeof(ExcludeAttribute)) is ExcludeAttribute))
				{
					object value = propertyInfo.GetValue(src);
					DataConverter.PropertySetValue(val, propertyInfo, value);
				}
			}
			return val;
		}

		public static void DeepCopy<T>(T src, T dst, List<string> except = null, bool isDeclaredOnly = true) where T : class, new()
		{
			PropertyInfo[] array = (isDeclaredOnly ? typeof(T).GetProperties(BindingFlags.DeclaredOnly | BindingFlags.Instance | BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic) : typeof(T).GetProperties());
			PropertyInfo[] array2 = array;
			foreach (PropertyInfo propertyInfo in array2)
			{
				if (!(except?.Contains(propertyInfo.Name) ?? false) && propertyInfo.CanWrite)
				{
					object value = propertyInfo.GetValue(dst);
					propertyInfo.SetValue(src, value);
				}
			}
		}

		public static T DeepCopy<T>(T dst, List<string> except = null, bool isDeclaredOnly = true) where T : class, new()
		{
			T val = new T();
			DataConverter.DeepCopy(val, dst, except, isDeclaredOnly);
			return val;
		}

		public static object ConvertObject(object obj, Type type)
		{
			if (type == null)
			{
				return obj;
			}
			if (obj == null)
			{
				return type.IsValueType ? Activator.CreateInstance(type) : null;
			}
			Type underlyingType = Nullable.GetUnderlyingType(type);
			if (type.IsInstanceOfType(obj))
			{
				return obj;
			}
			if ((underlyingType ?? type).IsEnum)
			{
				if (underlyingType != null && string.IsNullOrEmpty(obj.ToString()))
				{
					return null;
				}
				return Enum.Parse(underlyingType ?? type, obj.ToString());
			}
			if (typeof(IConvertible).IsAssignableFrom(underlyingType ?? type))
			{
				try
				{
					return Convert.ChangeType(obj, underlyingType ?? type, null);
				}
				catch
				{
					return (underlyingType == null) ? Activator.CreateInstance(type) : null;
				}
			}
			TypeConverter converter = TypeDescriptor.GetConverter(type);
			if (converter.CanConvertFrom(obj.GetType()))
			{
				return converter.ConvertFrom(obj);
			}
			ConstructorInfo constructor = type.GetConstructor(Type.EmptyTypes);
			if (constructor != null)
			{
				object obj3 = constructor.Invoke(null);
				PropertyInfo[] properties = type.GetProperties();
				Type type2 = obj.GetType();
				PropertyInfo[] array = properties;
				foreach (PropertyInfo propertyInfo in array)
				{
					PropertyInfo property = type2.GetProperty(propertyInfo.Name);
					if (propertyInfo.CanWrite && property != null && property.CanRead)
					{
						propertyInfo.SetValue(obj3, DataConverter.ConvertObject(property.GetValue(obj, null), propertyInfo.PropertyType), null);
					}
				}
				return obj3;
			}
			return obj;
		}

		public static object TypeFormat(Type type, object value)
		{
			if (value == null)
			{
				return null;
			}
			Type underlyingType = Nullable.GetUnderlyingType(type);
			Type type2 = underlyingType ?? type;
			if (type.IsInstanceOfType(value))
			{
				return value;
			}
			if (typeof(Enum).IsAssignableFrom(type2))
			{
				string text = value?.ToString().Trim();
				Attribute customAttribute = Attribute.GetCustomAttribute(type2, typeof(FlagsAttribute));
				if (customAttribute != null)
				{
					if (!text.Contains(','))
					{
						if (Enum.IsDefined(type2, value))
						{
							return Enum.Parse(type2, text);
						}
					}
					else if (!text.Replace(" ", "").Split(',').ToList()
						.Except(Enum.GetNames(type2).ToList())
						.Any())
					{
						return Enum.Parse(type2, text);
					}
				}
				else if (Enum.IsDefined(type2, text))
				{
					return Enum.Parse(type2, text);
				}
			}
			else if (typeof(IConvertible).IsAssignableFrom(type2))
			{
				try
				{
					if (!string.IsNullOrWhiteSpace(value?.ToString()))
					{
						return Convert.ChangeType(value, type2, null);
					}
				}
				catch
				{
					return null;
				}
			}
			return null;
		}

		public static T ConvertObjToT<T>(object value)
		{
			Type typeFromHandle = typeof(T);
			object obj = DataConverter.TypeFormat(typeFromHandle, value);
			if (obj != null)
			{
				return (T)obj;
			}
			return default(T);
		}

		public static void PropertySetValue(object instance, PropertyInfo property, object value)
		{
			if (!(property == null) && property.CanWrite)
			{
				object obj = DataConverter.TypeFormat(property.PropertyType, value);
				if (obj != null)
				{
					property.SetValue(instance, obj);
				}
			}
		}

		public static List<T> DataReaderToList<T>(IDataReader idataReader) where T : class
		{
			List<T> list = new List<T>();
			Type typeFromHandle = typeof(T);
			List<PropertyInfo> list2 = typeFromHandle.GetProperties().ToList();
			while (idataReader.Read())
			{
				T val = Activator.CreateInstance<T>();
				int i;
				for (i = 0; i < idataReader.FieldCount; i++)
				{
					PropertyInfo propertyInfo = list2.Find((PropertyInfo x) => x.Name == idataReader.GetName(i));
					if (!(propertyInfo == null) && Attribute.GetCustomAttribute(propertyInfo, typeof(ExcludeAttribute)) == null)
					{
						DataConverter.PropertySetValue(val, propertyInfo, idataReader.GetValue(i));
					}
				}
				list.Add(val);
			}
			return list;
		}

		public static ulong GetVersion(byte[] timestamp)
		{
			byte[] value = timestamp.Reverse().ToArray();
			return BitConverter.ToUInt64(value, 0);
		}

		public static string GetDtPk(DataTable dt)
		{
			DataColumn[] primaryKey = dt.PrimaryKey;
			if (primaryKey.Length == 0)
			{
				return "";
			}
			return primaryKey[0].ColumnName;
		}

		public static void ConvertToDtColsMuti<T>(List<T> colsEntities, ref DataTable dtSrc, bool isRefresh = false) where T : class, new()
		{
			if (dtSrc == null)
			{
				dtSrc = new DataTable();
			}
			PropertyInfo[] properties = typeof(T).GetProperties();
			if (!isRefresh)
			{
				foreach (T colsEntity in colsEntities)
				{
					PropertyInfo propertyInfo = properties.FirstOrDefault((PropertyInfo x) => BuildAttribute.GetCustomAb<T, CustomFlexCol>(x.Name) != null);
					if (propertyInfo != null)
					{
						DataColumn column = new DataColumn(propertyInfo.GetValue(colsEntity)?.ToString(), typeof(object));
						dtSrc.Columns.Add(column);
					}
				}
			}
			for (int i = 0; i < properties.Length; i++)
			{
				PropertyInfo propertyInfo2 = properties[i];
				CustomFlexCol customAb = BuildAttribute.GetCustomAb<T, CustomFlexCol>(propertyInfo2.Name);
				if (customAb != null)
				{
					string text = "";
					text = (string.IsNullOrWhiteSpace(customAb.ColName) ? propertyInfo2.Name : customAb.ColName);
					if (propertyInfo2.Name == text)
					{
						continue;
					}
				}
				DataRow dataRow = (isRefresh ? dtSrc.Rows[i - 1] : dtSrc.NewRow());
				for (int j = 0; j < colsEntities.Count; j++)
				{
					T obj = colsEntities[j];
					dataRow[j] = propertyInfo2.GetValue(obj);
				}
				if (!isRefresh)
				{
					dtSrc.Rows.Add(dataRow);
				}
			}
		}

		public static Tuple<bool, T> TryJsonDeserialize<T>(string info)
		{
			try
			{
				if (string.IsNullOrWhiteSpace(info))
				{
					return new Tuple<bool, T>(item1: false, default(T));
				}
				T item = JsonSerializer.Deserialize<T>(info);
				return new Tuple<bool, T>(item1: true, item);
			}
			catch (Exception)
			{
				return new Tuple<bool, T>(item1: false, default(T));
			}
		}

		public static string GetJsonFormat<T>(string info, Func<T, string> func = null)
		{
			if (string.IsNullOrWhiteSpace(info))
			{
				return "";
			}
			StringBuilder stringBuilder = new StringBuilder();
			Tuple<bool, List<T>> tuple = DataConverter.TryJsonDeserialize<List<T>>(info);
			if (tuple.Item1 && tuple.Item2 != null)
			{
				foreach (T item in tuple.Item2)
				{
					if (func != null)
					{
						string text = func(item) ?? "";
						stringBuilder.Append(text + ",");
					}
					else
					{
						stringBuilder.Append(item.ToString() + ",");
					}
				}
				if (stringBuilder.Length > 0)
				{
					stringBuilder = stringBuilder.Remove(stringBuilder.Length - 1, 1);
				}
				return stringBuilder.ToString();
			}
			return "";
		}

		public static string FiledWithUnderline(string name, int minCount = 1)
		{
			if (string.IsNullOrWhiteSpace(name))
			{
				return "";
			}
			if (name.Length == 1)
			{
				return name;
			}
			char[] array = name.ToCharArray();
			StringBuilder stringBuilder = new StringBuilder();
			StringBuilder stringBuilder2 = new StringBuilder();
			for (int i = 0; i < array.Length; i++)
			{
				char c = array[i];
				if (char.IsUpper(c) && i > 0 && stringBuilder2.Length >= minCount)
				{
					if (stringBuilder2.Length > 0)
					{
						StringBuilder stringBuilder3 = stringBuilder;
						StringBuilder.AppendInterpolatedStringHandler handler = new StringBuilder.AppendInterpolatedStringHandler(1, 1, stringBuilder3);
						handler.AppendFormatted(stringBuilder2);
						handler.AppendLiteral("_");
						stringBuilder3.Append(ref handler);
					}
					stringBuilder2.Clear();
				}
				stringBuilder2.Append(c);
			}
			string text = stringBuilder.ToString();
			if (stringBuilder2.Length > 0)
			{
				text += stringBuilder2;
			}
			else if (text.EndsWith("_"))
			{
				text = text.Remove(text.Length - 1, 1);
			}
			return text;
		}

		public static string FiledWithHump(string name)
		{
			if (string.IsNullOrWhiteSpace(name))
			{
				return "";
			}
			if (!name.Contains("_"))
			{
				return name.FirstToUpper();
			}
			IEnumerable<string> enumerable = from x in name.Split('_')
				where x.Trim() != ""
				select x;
			StringBuilder stringBuilder = new StringBuilder();
			foreach (string item in enumerable)
			{
				stringBuilder.Append(item.FirstToUpper());
			}
			return stringBuilder.ToString();
		}

		public static DataTable ConvertCusAbToDb<T, TAb>() where T : class where TAb : Attribute
		{
			DataTable dataTable = new DataTable();
			dataTable.Columns.Add(new DataColumn("Name", typeof(string)));
			PropertyInfo[] properties = typeof(TAb).GetProperties();
			PropertyInfo[] array = properties;
			foreach (PropertyInfo propertyInfo in array)
			{
				dataTable.Columns.Add(new DataColumn(propertyInfo.Name));
			}
			PropertyInfo[] properties2 = typeof(T).GetProperties();
			PropertyInfo[] array2 = properties2;
			foreach (PropertyInfo propertyInfo2 in array2)
			{
				TAb val = Attribute.GetCustomAttribute(propertyInfo2, typeof(TAb)) as TAb;
				if (val != null)
				{
					DataRow dataRow = dataTable.NewRow();
					dataRow["Name"] = propertyInfo2.Name;
					PropertyInfo[] array3 = properties;
					foreach (PropertyInfo propertyInfo3 in array3)
					{
						dataRow[propertyInfo3.Name] = propertyInfo3.GetValue(val, null)?.ToString();
					}
					dataTable.Rows.Add(dataRow);
				}
			}
			return dataTable;
		}
	}
}
